package com.cqupt.slewing

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.cqupt.slewing.room.ItemViewModel
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class TrackFragment : Fragment() {
    private lateinit var itemViewModel: ItemViewModel
    private lateinit var myViewModel: MyViewModel

    private lateinit var textViewMac: TextView
    private lateinit var textViewName: TextView
    private lateinit var textViewStatus: TextView
    private lateinit var myView: MyView


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_track, container, false)
        textViewMac = view.findViewById(R.id.textViewMac)
        textViewStatus = view.findViewById(R.id.textViewStatus)
        textViewName = view.findViewById(R.id.textViewName)
        myView = view.findViewById(R.id.myView)
        lifecycle.addObserver(myView)
        return view
    }

    override fun onViewStateRestored(savedInstanceState: Bundle?) {
        super.onViewStateRestored(savedInstanceState)
        itemViewModel = ViewModelProvider(requireActivity())[ItemViewModel::class.java]
        myViewModel = ViewModelProvider(requireActivity())[MyViewModel::class.java]

        val bottomNavigationView: BottomNavigationView =
            requireActivity().findViewById(R.id.bottom_nav_view)
        bottomNavigationView.visibility = View.GONE //隐藏底部导航栏

        lifecycleScope.launch(Dispatchers.IO) {
            val item = arguments?.getString("macID")?.let { itemViewModel.getItemByMac(it) }!!
            myView.macID = item.macID
            textViewMac.text = item.macID
            textViewStatus.text = item.status
//            textViewLocation.text = myViewModel.getDevLocation(item.macID)
        }

    }


}